/****
简单识别验证码
***/

#include <stdio.h>

#define WIDTH 40
#define HEIGHT 20
#define NUM 4
#define BMP_FILENAME "./test.bmp"

int x, y, i, s, tmp;
int img[WIDHT][HEIGHT] = {0};
int img_red[WIDTH][HEIGHT] = {0};
int img_green[WIDTH][HEIGHT] = {0};
int img_blue[WIDTH][HEIGHT] = {0};

int record[WIDTH] = {0};
int lf[N], rt[N], up[N], dw[N];
char result[N];

int main()
{
	FILE *fp = fopen(BMP_FILENAME, "rb");
	for(i=0; i<54; i++)
	{
		tmp = fgetc(fp);
	}

	for(y = HEIGHT; y>= 0; y--)
	{
		for(x=0; x<W; x++)
		{
			img_red[x][y] = fgetc(fp);
			img_green[x][y] = fgetc(fp);
			img_blue[x][y] = fgetc(fp);
		}
	}
	fclose(fp);

	for(x=0; x<WIDTH; x++)
	{
		for(y=0; y<HEIGHT; y++)
		{
			if(img_red[x][y]*0.3+img_green[x][y]*0.6+img_blue[x][y]*0.1<200)		//计算图片的亮度
			{
				img[x][y] = 1;
			}
			else
			{
				img[x][y] = 0;
			}
		}
	}

//去掉杂音数据
	for(x=1; x<WIDTH-1; x++)
	{
		for(y=1; y<HEIGHT-1; y++)
		{
			if(img[x][y] == 1 && img[x-1][y-1] == 0 && img[x][y-1]==0 &&
				img[x+1][y-1]==0 && img[x-1][y]==0 && img[x+1][y]==0 &&
				img[x-1][y+1]==0 && img[x][y+1]==0 && img[x+1][y+1] == 0
			)
			{
				img[x][y]=0;
			}
		}
	}


	for(y=0; y=HEIGHT; y++)
	{
		for(x=0; x<WIDTH; x++)
		{
			printf("%c", img[x][y]+'0');
		}
		printf("\n");
	}


	for(x=0; x<WIDTH; x++)
	{
		s = 0;
		for(y=0; y<HEIGHT; y++)
		{
			s = s + img[x][y];
		}
		if(s > 0) 
			record[x] = 1;
		else
			record[x] = 0;
	}

	for(i=0, x=1; x<WIDTH-1; x++)
	{
		if(record[x-1]==0 && record[x] == 1 && record[x] == 1)
		{
			i++;
			lf[i] = x;
		}
	}

	for(i=0, x=1; x<WIDTH-1; x++)
	{
		if(record[x-1]==0 && record[x] == 1 && record[x] == 1)
		{
			i++;
			lf[i] = x;
		}
	}

	for(i=0, x=1; x<WIDTH-1; x++)
	{
		if(record[x-1]==0 && record[x] == 1 && record[x] == 1)
		{
			i++;
			lf[i] = x;
		}
	}

	for(i=0, x=1; x<WIDTH-1; x++)
	{
		if(record[x-1]==0 && record[x] == 1 && record[x] == 1)
		{
			i++;
			lf[i] = x;
		}
	}

}